package com.erp.erp_ui.CodeGeneration;

import com.erp.erp_entitys.CodeGeneration.CodeGenerationEntity;
import com.erp.erp_entitys.CodeGeneration.CodeGenerationExample;
import com.erp.erp_servers.CodeGeneration.ICodeGenerationService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.List;

@Controller
@RequestMapping(value = "codegeneration")
public class CodeGenerationController extends Base {
    @Autowired
    ICodeGenerationService codeGenerationService;


    /**
     *
     * @param type  类型（1：客户，2：供应商）
     * @param prefix  前缀
     * @param suffix 后缀
     * @return
     */
    @RequestMapping(value = "getCode")
    @ResponseBody
    public R<String> getCode(int type,String prefix,String suffix ) {
        try {
            String code="";
            CodeGenerationEntity cge=new CodeGenerationEntity();
            cge.setPrefix(prefix);
            cge.setSuffix(suffix);
            cge.setType(type);
            String ct=System.currentTimeMillis()+"";
            cge.setCreateTime(ct);
            cge.setCode(0);
            int insert = codeGenerationService.insert(cge);
            //上在是将传入的参数存表
            if(insert==1){
                //存入成功后根据时间戳查询
                CodeGenerationExample example=new CodeGenerationExample();
                CodeGenerationExample.Criteria criteria = example.createCriteria();
                criteria.andCreateTimeEqualTo(ct);
                List<CodeGenerationEntity> codeGenerationEntities = codeGenerationService.selectByExample(example);
                CodeGenerationEntity codeGenerationEntity = codeGenerationEntities.get(0);
                //查询当前数量
                CodeGenerationExample example2=new CodeGenerationExample();
                CodeGenerationExample.Criteria criteria1 = example2.createCriteria();
                criteria1.andPrefixEqualTo(prefix);
                criteria1.andSuffixEqualTo(suffix);
                criteria1.andTypeEqualTo(type);
                long l = codeGenerationService.countByExample(example2);
                //数量加1进行更新
               // l++;
                CodeGenerationEntity record=new CodeGenerationEntity();
                record.setId(codeGenerationEntity.getId());
                record.setCode(Integer.parseInt(l+""));
                codeGenerationService.updateByPrimaryKeySelective(record);
                //格式化code，至少5位
                int cd=Integer.parseInt(l+"");
                int n=5;
                String STR_FORMAT = "00000";
                if(cd>=99999){
                    n=(cd+"").length()+1;
                    for(int i=0;i<(n+"").length()-5;i++){
                        STR_FORMAT+="0";
                    }
                }
                DecimalFormat df = new DecimalFormat(STR_FORMAT);
                String format = df.format(cd);
                code=codeGenerationEntity.getPrefix()+ format+codeGenerationEntity.getSuffix();

            }
            return R.build(code);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

}
